﻿using System;
using System.IO.Ports;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplication1
{
    public class Program
    {
        public static void Main()
        {
            Program.Hc06Programmer();
            while (true)
            {
                System.Threading.Thread.Sleep(10000);
            }
        }

        public static void Btm222Programmer()
        {
            // ATL0   4800
            // ATL1   9600
            // ATL2  19200
            // ATL3  38400
            // ATL4  57600
            // ATL5 115200
            // ATL6 230400
            // ATL7 460800

            SerialPort _serialPort = new SerialPort("COM1", 9600);
            _serialPort.Open();
            bool cw = _serialPort.CanWrite;
            String s = "";
            byte[] baWrite;

            // check connection
            baWrite = System.Text.UTF8Encoding.UTF8.GetBytes("ATL?");
            _serialPort.Write(baWrite, 0, baWrite.Length);
            // write CR
            //_serialPort.WriteByte((byte)13);
            _serialPort.Flush();

            s = Program.ReadLine(_serialPort);

            // write new baud rate
            baWrite = System.Text.UTF8Encoding.UTF8.GetBytes("ATL5");
            _serialPort.Write(baWrite, 0, baWrite.Length);
            //_serialPort.WriteByte((byte)13);
            _serialPort.Flush();

            s = Program.ReadLine(_serialPort);

            _serialPort.Close();
        }

        public static void Hc06Programmer()
        {
            // BAUD4   9600
            // BAUD5  19200
            // BAUD6  38400
            // BAUD7  57600
            // BAUD8 115200

            SerialPort _serialPort = new SerialPort("COM1", 9600);
            _serialPort.Open();
            bool cw = _serialPort.CanWrite;
            String s = "";
            byte[] baWrite;

            // check connection
            baWrite = System.Text.UTF8Encoding.UTF8.GetBytes("AT");
            _serialPort.Write(baWrite, 0, baWrite.Length);
            // write CR
            //_serialPort.WriteByte((byte)13);
            _serialPort.Flush();

            byte[] buffer = new byte[2];

            _serialPort.Read(buffer, 0, buffer.Length);
            s = String.Concat(s, (char)buffer[0]);

            // write new baud rate
            baWrite = System.Text.UTF8Encoding.UTF8.GetBytes("AT+BAUD8");
            _serialPort.Write(baWrite, 0, baWrite.Length);
            //_serialPort.WriteByte((byte)13);
            _serialPort.Flush();

            s = Program.ReadLine(_serialPort);

            _serialPort.Close();
        }

        /// <summary>
        /// Reads a line from SerialPort, newline is truncated, empty lines are ignored
        /// Method call is blocking until not empty line followed by newline is read from SerialPort
        /// </summary>
        /// <returns>Data read from SerialPort</returns>
        private static String ReadLine(SerialPort port)
        {
            String s = "";
            byte[] buffer = new byte[1];

            port.Read(buffer, 0, buffer.Length);
            // ascii representation 10 is \n, 13 is \r
            while (buffer[0] != 10 && buffer[0] != 13)
            {
                s = String.Concat(s, (char)buffer[0]);
                port.Read(buffer, 0, buffer.Length);
            }

            // drop empty lines by recursive call
            if (s.Length == 0)
            {
                return Program.ReadLine(port);
            }
            return s;
        }

    }
}
